package stack

import (
	"bytes"
	"fmt"
	"go-guide/data-structures/array"
)

type ArrayStack struct {
	arr *array.Array
}

func (a *ArrayStack) Size() int {
	return a.arr.Size()
}

func (a *ArrayStack) Empty() bool {
	return a.arr.Empty()
}

func (a *ArrayStack) Push(e interface{}) {
	a.arr.AddLast(e)
}

func (a *ArrayStack) Pop() interface{} {
	return a.arr.Remove(a.arr.Size() - 1)
}

func (a *ArrayStack) Peek() interface{} {
	return a.arr.Get(a.arr.Size() - 1)
}

func (a *ArrayStack) String() string {
	var buffer bytes.Buffer

	buffer.WriteString("Stack: ")
	buffer.WriteString("[")
	for i := 0; i < a.arr.Size(); i++ {
		buffer.WriteString(fmt.Sprint(a.arr.Get(i)))
		if i != a.arr.Size()-1 {
			buffer.WriteString(", ")
		}
	}
	buffer.WriteString("] top")

	return buffer.String()
}
